gdkarray: Add a "stolen" boolean to splice()
authorBenjamin Otte <otte@redhat.com>
Wed, 23 Dec 2020 19:04:29 +0000 (20:04 +0100)
committerBenjamin Otte <otte@redhat.com>
Thu, 24 Dec 2020 05:38:45 +0000 (06:38 +0100)
If set to TRUE, does not call the free func for the removed items.

This can be used to move items between arrays without having to do the
refcounting dance.

gdk/gdkarrayimpl.c
gtk/gtkactionmuxer.c
gtk/gtkcssselector.c
gtk/gtkmultifilter.c
gtk/gtkmultisorter.c
gtk/gtksnapshot.c
gtk/gtkstringlist.c
testsuite/gdk/arrayimpl.c

index 003c67fd59fd2c92cae395b5c27562fd5752bde3..a18ab5e33d78cd23d121560e9f34b02cb81cae49 100644 (file)
@@ -182,6 +182,7 @@ G_GNUC_UNUSED static inline void
 gdk_array(splice) (GdkArray *self,
                    gsize      pos,
                    gsize      removed,
+                   gboolean   stolen,
                    _T_       *additions,
                    gsize      added)
 {
@@ -192,8 +193,9 @@ gdk_array(splice) (GdkArray *self,
   g_assert (pos + removed <= size);
   remaining = size - pos - removed;
 
-  gdk_array(free_elements) (gdk_array(index) (self, pos),
-                            gdk_array(index) (self, pos + removed));
+  if (!stolen)
+    gdk_array(free_elements) (gdk_array(index) (self, pos),
+                              gdk_array(index) (self, pos + removed));
 
   gdk_array(reserve) (self, size - removed + added);
 
@@ -225,9 +227,9 @@ gdk_array(set_size) (GdkArray *self,
 {
   gsize old_size = gdk_array(get_size) (self);
   if (new_size > old_size)
-    gdk_array(splice) (self, old_size, 0, NULL, new_size - old_size);
+    gdk_array(splice) (self, old_size, 0, FALSE, NULL, new_size - old_size);
   else
-    gdk_array(splice) (self, new_size, old_size - new_size, NULL, 0);
+    gdk_array(splice) (self, new_size, old_size - new_size, FALSE, NULL, 0);
 }
 
 G_GNUC_UNUSED static void
@@ -241,6 +243,7 @@ gdk_array(append) (GdkArray *self,
   gdk_array(splice) (self, 
                      gdk_array(get_size) (self),
                      0,
+                     FALSE,
 #ifdef GDK_ARRAY_BY_VALUE
                      value,
 #else
index fad926ceaf71857738a67e8f24f83429fb2455b0..fb3c4fb5a3f6969c73eba8cacbf12f1737459f3a 100644 (file)
@@ -100,7 +100,7 @@ gtk_accels_remove (GtkAccels  *accels,
 
   position = gtk_accels_find (accels, action_and_target);
   if (position < gtk_accels_get_size (accels))
-    gtk_accels_splice (accels, position, 1, NULL, 0);
+    gtk_accels_splice (accels, position, 1, FALSE, NULL, 0);
 }
 
 /*< private >
index b8ef92a9ae32187018e3a2d5f58cd77fd7eb3497..f04a6fe7c5b32e77e4fb5acb93ef1c301ca4615b 100644 (file)
@@ -167,7 +167,7 @@ gtk_css_selector_matches_insert_sorted (GtkCssSelectorMatches *matches,
         break;
     }
 
-  gtk_css_selector_matches_splice (matches, i, 0, (gpointer[1]) { data }, 1);
+  gtk_css_selector_matches_splice (matches, i, 0, FALSE, (gpointer[1]) { data }, 1);
 }
 
 static inline gboolean
index 8211295fb2373cf798f74bcfc6713245471790df..3550d90c585988b76e456f1ef90750361feb7fb2 100644 (file)
@@ -206,7 +206,7 @@ gtk_multi_filter_remove (GtkMultiFilter *self,
 
   filter = gtk_filters_get (&self->filters, position);
   g_signal_handlers_disconnect_by_func (filter, gtk_multi_filter_changed_cb, self);
-  gtk_filters_splice (&self->filters, position, 1, NULL, 0);
+  gtk_filters_splice (&self->filters, position, 1, FALSE, NULL, 0);
 
   gtk_filter_changed (GTK_FILTER (self),
                       GTK_MULTI_FILTER_GET_CLASS (self)->removal_change);
index 5690d6d1d09b6fb11022b5367b865d80f307229d..a733f5a275e6da868d4dc9919122e5447696ee91 100644 (file)
@@ -432,7 +432,7 @@ gtk_multi_sorter_remove (GtkMultiSorter *self,
 
   sorter = gtk_sorters_get (&self->sorters, position);
   g_signal_handlers_disconnect_by_func (sorter, gtk_multi_sorter_changed_cb, self);
-  gtk_sorters_splice (&self->sorters, position, 1, NULL, 0);
+  gtk_sorters_splice (&self->sorters, position, 1, FALSE, NULL, 0);
 
   gtk_sorter_changed_with_keys (GTK_SORTER (self),
                                 GTK_SORTER_CHANGE_LESS_STRICT,
index 7c5c74484237ef16cd91664b408152d3929289eb..e01ce5ec68f2c10778c9d57516a709d87e85fee1 100644 (file)
@@ -1386,7 +1386,7 @@ gtk_snapshot_pop_one (GtkSnapshot *snapshot)
 
       /* Remove all the state's nodes from the list of nodes */
       g_assert (state->start_node_index + state->n_nodes == gtk_snapshot_nodes_get_size (&snapshot->nodes));
-      gtk_snapshot_nodes_splice (&snapshot->nodes, state->start_node_index, state->n_nodes, NULL, 0);
+      gtk_snapshot_nodes_splice (&snapshot->nodes, state->start_node_index, state->n_nodes, FALSE, NULL, 0);
     }
   else
     {
@@ -1400,7 +1400,7 @@ gtk_snapshot_pop_one (GtkSnapshot *snapshot)
       g_assert (previous_state->start_node_index + previous_state->n_nodes == gtk_snapshot_nodes_get_size (&snapshot->nodes));
     }
 
-  gtk_snapshot_states_splice (&snapshot->state_stack, state_index, 1, NULL, 0);
+  gtk_snapshot_states_splice (&snapshot->state_stack, state_index, 1, FALSE, NULL, 0);
 
   return node;
 }
index 64e2e56155f6de2c8491eb9e0878f842cf719e8f..c271ed91c16865e3287716b6515f61be9cb3ca45 100644 (file)
@@ -471,7 +471,7 @@ gtk_string_list_splice (GtkStringList      *self,
   else
     n_additions = 0;
 
-  objects_splice (&self->items, position, n_removals, NULL, n_additions);
+  objects_splice (&self->items, position, n_removals, FALSE, NULL, n_additions);
 
   for (i = 0; i < n_additions; i++)
     {
index 286f64d6783c711f0064609b6e8e9440d695f37e..77494fbf7042ee268536e7e132226de2da6d3896 100644 (file)
@@ -77,7 +77,7 @@ gdk_array(test_splice) (void)
       for (j = 0; j < add; j++)
         sum += ++additions[j];
 
-      gdk_array(splice) (&v, pos, remove, additions, add);
+      gdk_array(splice) (&v, pos, remove, FALSE, additions, add);
       {
         gsize total = 0;
         for (j = 0; j < gdk_array(get_size) (&v); j++)